package shuzu

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func PartSum() {
	var a int
	fmt.Scanf("%d\n", &a)
	nums := make([]int, a)
	for i := 0; i < a; i++ {
		fmt.Scanf("%d\n", &nums[i])
	}
	ans := make([]int, 0)
	perfixs := make([]int, a)
	cur := 0
	for i := 0; i < a; i++ {
		cur += nums[i]
		perfixs[i] = cur
	}
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		text := scanner.Text()
		fields := strings.Fields(text)
		if len(fields) != 2 {
			break
		}
		a, _ := strconv.Atoi(fields[0])
		b, _ := strconv.Atoi(fields[1])
		// 计算所有前缀和，输出对应结果
		num := 0
		if a == 0 {
			num = perfixs[b]
		} else {
			num = perfixs[b] - perfixs[a-1]
		}
		ans = append(ans, num)
	}
	for i := 0; i < len(ans); i++ {
		fmt.Println(ans[i])
	}
}

func PartSum1() {
	var a int
	fmt.Print("请输入要输入的整数个数：")
	_, err := fmt.Scanf("%d\n", &a)
	if err != nil || a < 0 {
		fmt.Println("输入无效，请输入一个非负整数。")
		return
	}

	nums := make([]int, a)
	for i := 0; i < a; i++ {
		var b int
		fmt.Printf("请输入第 %d 个整数：", i+1)
		_, err := fmt.Scanf("%d\n", &b)
		if err != nil {
			fmt.Println("输入无效，程序终止。")
			return
		}
		nums[i] = b
	}

	fmt.Printf("你输入了：%d 个数字，分别是：%v\n", a, nums)
}
